﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ViCode_LeVi.Data;
using ViCode_LeVi.Reports;

namespace ViCode_LeVi.Controls
{
    /// <summary>
    /// Interaction logic for VMTabBalanceAndOthers_PROFITANDLOSS.xaml
    /// </summary>
    public partial class VMTabBalanceAndOthers_PROFITANDLOSS : UserControl
    {
        public VMTabBalanceAndOthers_PROFITANDLOSS()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(VMTabBalanceAndOthers_Balance_Loaded);
            this.DataContextChanged += new DependencyPropertyChangedEventHandler(VMTabBalanceAndOthers_PROFITANDLOSS_DataContextChanged);
        }

        void VMTabBalanceAndOthers_PROFITANDLOSS_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            RefreshView();
        }
        Step_BalanceAndOthers_PROFIT_LOSS_STATEMENT _PROFIT = null;
        void VMTabBalanceAndOthers_Balance_Loaded(object sender, RoutedEventArgs e)
        {
            RefreshView();
        }
        private void RefreshView()
        {
            _PROFIT = this.DataContext as Step_BalanceAndOthers_PROFIT_LOSS_STATEMENT;
            if (_PROFIT != null)
            {
                _PROFIT.RefreshData();
                GenUI(_PROFIT);
                SetupButton_AddNew();
            }
        }
        #region Gen
        private void GenUI(Step_BalanceAndOthers_PROFIT_LOSS_STATEMENT balance)
        {
            if (balance == null)
                return;
            if (balance.Items_INCOME == null || balance.Items_INCOME.Length == 0)
            {
                balance.AddNew(balance.GROUP_INCOME);
            }
            GenUI(Panel_INCOME, balance.Items_INCOME);

            if (balance.Items_COSTS == null || balance.Items_COSTS.Length == 0)
            {
                balance.AddNew(balance.GROUP_COSTS);
            }
            GenUI(Panel_COSTS, balance.Items_COSTS);

            if (balance.Items_OPERATINGEXPENSES == null || balance.Items_OPERATINGEXPENSES.Length == 0)
            {
                balance.AddNew(balance.GROUP_OPERATINGEXPENSES);
            }
            GenUI(Panel_OPERATINGEXPENSES, balance.Items_OPERATINGEXPENSES);           
        }
        private void GenUI(Panel panel, Step_BalanceAndOthers_PROFIT_LOSS_STATEMENT_Item[] items)
        {
            if (items == null || items.Length == 0)
            {
                panel.Children.Clear();
            }
            else
            {
                ////Collapse all
                //foreach(FrameworkElement child in panel.Children)
                //{
                //    child.Visibility = Visibility.Collapsed;
                //}
                //add
                var children = panel.Children.Cast<FrameworkElement>().ToArray();
                List<FrameworkElement> listInUI = new List<FrameworkElement>();
                foreach (var item in items)
                {
                    var child = children.FirstOrDefault(d => d.DataContext == item);
                    if (child == null)
                    {
                        child = new VMTabBalanceAndOthers_PROFITANDLOSS_Item()
                        {
                            DataContext = item
                              ,
                            Action_Remove = (removeItem) =>
                            {
                                if (_PROFIT != null)
                                {
                                    if (MessageBox.Show("Do you want to remove this item?", "Confirm", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                                    {
                                        _PROFIT.Remove(removeItem);
                                        if (removeItem.Parent != null)
                                            removeItem.Parent.SendPropertyChanged_Total();
                                        GenUI(_PROFIT);
                                    }
                                }
                            }
                        };
                        panel.Children.Add(child);
                    }
                    listInUI.Add(child);
                }
                //remove old
                List<FrameworkElement> listWillRemove = new List<FrameworkElement>();
                foreach (FrameworkElement child in panel.Children)
                {
                    if (!listInUI.Contains(child))
                        listWillRemove.Add(child);
                }
                foreach (var removeItem in listWillRemove)
                {
                    panel.Children.Remove(removeItem);
                }
            }
        }
        #endregion

        #region Button add new
        private void SetupButton_AddNew()
        {
            Button_AddNew_INCOME.Click -= new RoutedEventHandler(Button_AddNew_INCOME_Click);
            Button_AddNew_INCOME.Click += new RoutedEventHandler(Button_AddNew_INCOME_Click);

            Button_AddNew_COSTS.Click -= new RoutedEventHandler(Button_AddNew_COSTS_Click);
            Button_AddNew_COSTS.Click += new RoutedEventHandler(Button_AddNew_COSTS_Click);

            Button_AddNew_OPERATINGEXPENSES.Click -= new RoutedEventHandler(Button_AddNew_OPERATINGEXPENSES_Click);
            Button_AddNew_OPERATINGEXPENSES.Click += new RoutedEventHandler(Button_AddNew_OPERATINGEXPENSES_Click);
        }

        void Button_AddNew_INCOME_Click(object sender, RoutedEventArgs e)
        {
            if (_PROFIT != null)
                Button_AddNew(_PROFIT.GROUP_INCOME);
        }
        void Button_AddNew_COSTS_Click(object sender, RoutedEventArgs e)
        {
            if (_PROFIT != null)
                Button_AddNew(_PROFIT.GROUP_COSTS);
        }
        void Button_AddNew_OPERATINGEXPENSES_Click(object sender, RoutedEventArgs e)
        {
            if (_PROFIT != null)
                Button_AddNew(_PROFIT.GROUP_OPERATINGEXPENSES);
        }
        
        void Button_AddNew(string grpName)
        {
            Button_AddNew(grpName, false);
        }
        void Button_AddNew(string grpName, bool just1Item)
        {
            if (_PROFIT != null)
            {
                _PROFIT.AddNew(grpName);
                GenUI(_PROFIT);
            }
        }
        #endregion

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Report_ProfitAndLossStatement rep = new Report_ProfitAndLossStatement();
            var datasource = new List<Step_BalanceAndOthers_PROFIT_LOSS_STATEMENT>();
            datasource.Add(_PROFIT);
            frmViewReport frm = new frmViewReport(rep, datasource);
            frm.Show();
        }
    }
}
